//
//  JCSelectMemberCell.swift
//  JChat
//
//  Created by JIGUANG on 2017/5/11.
//  Copyright © 2017年 HXHG. All rights reserved.
//

import UIKit
import JMessage
import MTCategory

class JCSelectMemberCell: UITableViewCell {

    var icon: UIImage? {
        get {
            return avatorView.image
        }
        set {
            avatorView.image = newValue
        }
    }
    
    var selectIcon: UIImage? {
        get {
            return selectIconView.image
        }
        set {
            selectIconView.image = newValue
        }
    }
    
    var title: String? {
        get {
            return usernameLabel.text
        }
        set {
            usernameLabel.text = newValue
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor.theme.background
        selectionStyle = .none
        _init()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        _init()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        _init()
    }
    
    private lazy var avatorView: UIImageView = UIImageView()
    private lazy var usernameLabel: UILabel = UILabel()
    private lazy var selectIconView: UIImageView = UIImageView()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        return v
    }()
    
    public func bindDate(_ user : JMSGUser) {
        title = user.displayName()
        icon = UIImage(named: defaultPlaceholderIcon60x60)
//        user.thumbAvatarData({ (data, name, error) in
//            if data != nil {
//                let image = UIImage(data: data!)
//                self.icon = image
//            }
//        })
        self.avatorView.mtSetImage(user.avatar, nil)
    }
    
    var hiddenLine: Bool = false {
        didSet {
            lineView.isHidden = hiddenLine
        }
    }
    
    //MARK: - private func
    private func _init() {
        
        let image = UIImage.loadImage("com_icon_unselect")
        
        selectIconView.image = image
        
        usernameLabel.textColor = UIColor.text.xf3f3f3
        usernameLabel.font = PingFangSCRegularFont(14)
        
        avatorView.layer.cornerRadius = 22
        avatorView.layer.masksToBounds = true
        avatorView.contentMode = .scaleAspectFill
        
        contentView.addSubview(selectIconView)
        contentView.addSubview(avatorView)
        contentView.addSubview(usernameLabel)
        contentView.addSubview(lineView)
        
        selectIconView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
            make.width.height.equalTo(22)
        }

        avatorView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(16)
            make.width.height.equalTo(44)
        }

        usernameLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(avatorView.snp.right).offset(11)
            make.right.equalTo(selectIconView.snp.left).offset(-11)
            make.height.equalTo(25)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
//        selectIconView.snp.makeConstraints { (make) in
//            make.centerY.equalToSuperview()
//            make.left.equalToSuperview().offset(16)
//            make.width.height.equalTo(22)
//        }
//
//        avatorView.snp.makeConstraints { (make) in
//            make.centerY.equalToSuperview()
//            make.left.equalTo(selectIconView.snp.right).offset(11)
//            make.width.height.equalTo(36)
//        }
//
//        usernameLabel.snp.makeConstraints { (make) in
//            make.centerY.equalToSuperview()
//            make.left.equalTo(avatorView.snp.right).offset(11)
//            make.right.equalToSuperview().offset(-11)
//            make.height.equalTo(25)
//        }
        
    }

}
